using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;


namespace _2DGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Bullet : GameObject
    {

        Player bulletParent;        // Who fired this bullet?  (Could be a ship, could be a turret etc.)


        public Bullet(String filename, Player inBulletParent, Vector2 myStartPosition, Vector2 myVelocity)
            : base(filename, SpriteBlendMode.Additive)
        {
            // Keep a reference to the ship that fired it, so we can avoid instant collisions!
            bulletParent = inBulletParent;

            // The gun should tell the bullet where to fire; the bullet shouldn't have to ask.
            position = myStartPosition;

            // Starting velocity.
            velocity = myVelocity;

            // Use the initial velocity to calculate the rotation.  We'll do this in the update too.
            rotation = (float) Math.Atan2(velocity.X, -velocity.Y);

            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public override void Update(GameTime gameTime)
        {
            velocity += Resources.World.Gravity;

            base.Update(gameTime);

        }



        public override void Draw()
        {
            base.Draw();
        }


        public override void TouchMap(List<Map.CollisionInfo> collisions)
        {
            BulletExplode();

            base.TouchMap(collisions);
        }

        public override bool Touch(GameObject otherObject)
        {
            if (otherObject is Ship)
            {
                BulletExplode();

                ((Ship)otherObject).DamagedBy(bulletParent, 1);

                Resources.World.RemoveObject(this);
            }

            return base.Touch(otherObject);
        }

        private void BulletExplode()
        {
            Color particleColor = new Color();

            // Make reddy orange
            particleColor.R = (byte)(128 + Resources.Rand.Next(127));
            particleColor.G = (byte)Resources.Rand.Next(127);
            particleColor.B = 0;
            particleColor.A = 255;

            float particleScale = (float)Resources.Rand.NextDouble() * 2.0f;

            Resources.World.Map.DestroyTerrain(position, 20.0f);

            Resources.World.AddObject(new EffectObject(
                "dot_8",
                SpriteBlendMode.Additive,
                position,
                Vector2.Zero,
                Vector2.Zero,
                particleColor,
                40.0f,
                -40.0f / 0.2f,
                0.2f));
        }
    }
}